import { Injectable } from '@nestjs/common';
import { CreateCategoryDto } from './dto/create-category.dto';
import { UpdateCategoryDto } from './dto/update-category.dto';
import { Repository } from 'typeorm';
import { Category } from 'src/entities/category.entity';
import { InjectRepository } from '@nestjs/typeorm';

@Injectable()
export class CategoryService {

  constructor(

    @InjectRepository(Category)
    private categoryRepository: Repository<Category>

  ) {}

  async create(createCategoryDto: CreateCategoryDto) {
    const category = this.categoryRepository.create(createCategoryDto)

    const res = this.categoryRepository.insert(category)

    return res;
  }

  async findAll() {
    const res = this.categoryRepository.find()

    return res;
  }

  async findOne(categoryId: string) {
    const res = await this.categoryRepository.findOne({
      where: { categoryId },
      relations: ['productList'],
    })
    return res;
  }

  update(id: number, updateCategoryDto: UpdateCategoryDto) {
    return `This action updates a #${id} category`;
  }

  remove(id: number) {
    return `This action removes a #${id} category`;
  }
}
